#include "AIPlayer.h"

AIPlayer::AIPlayer(core::stringw pName, int mainAttrib, int rank)
{
	m_AIPlayer = new Player(pName, mainAttrib, rank);
	lastMove = MOVE_COUNT;
	movetries = 0;
}

AIPlayer::~AIPlayer()
{
	printf("AIPLAYER CLASS DESTROYED!\n");
}

void AIPlayer::SetPlayerSetting()
{
	switch(m_AIPlayer->GetRank())
	{
	case RANK_A:
		{
			Setting_A();
			break;
		}
	case RANK_B:
		{
			Setting_B();
			break;
		}
	case RANK_C:
		{
			Setting_C();
			break;
		}
	case RANK_D:
		{
			Setting_D();
			break;
		}
	case RANK_E:
		{
			Setting_E();
			break;
		}
	case RANK_F:
		{
			Setting_F();
			break;
		}
	}
	m_AIPlayer->UpdatePlayerStats();
}

Player* AIPlayer::GetInstance()
{
	return m_AIPlayer;
}

void AIPlayer::Setting_A()
{
	m_AIPlayer->SetAttribute(ATTRIB_MATH, 30);
	m_AIPlayer->SetAttribute(ATTRIB_SCIENCE, 30);
	m_AIPlayer->SetAttribute(ATTRIB_PE, 30);
}

void AIPlayer::Setting_B()
{
	m_AIPlayer->SetAttribute(ATTRIB_MATH, 25);
	m_AIPlayer->SetAttribute(ATTRIB_SCIENCE, 25);
	m_AIPlayer->SetAttribute(ATTRIB_PE, 25);
}

void AIPlayer::Setting_C()
{
	m_AIPlayer->SetAttribute(ATTRIB_MATH, 20);
	m_AIPlayer->SetAttribute(ATTRIB_SCIENCE, 20);
	m_AIPlayer->SetAttribute(ATTRIB_PE, 20);
}

void AIPlayer::Setting_D()
{
	m_AIPlayer->SetAttribute(ATTRIB_MATH, 15);
	m_AIPlayer->SetAttribute(ATTRIB_SCIENCE, 15);
	m_AIPlayer->SetAttribute(ATTRIB_PE, 15);
}

void AIPlayer::Setting_E()
{
	m_AIPlayer->SetAttribute(ATTRIB_MATH, 10);
	m_AIPlayer->SetAttribute(ATTRIB_SCIENCE, 10);
	m_AIPlayer->SetAttribute(ATTRIB_PE, 10);
}

void AIPlayer::Setting_F()
{
	m_AIPlayer->SetAttribute(ATTRIB_MATH, 5);
	m_AIPlayer->SetAttribute(ATTRIB_SCIENCE, 5);
	m_AIPlayer->SetAttribute(ATTRIB_PE, 5);
}

EMOVE AIPlayer::GenerateMove(Player* pA)
{
	EMOVE retVal = MOVE_NONE;
	//Naging Defensive eh
	int x_diff = pA->curX - m_AIPlayer->curX;
	int y_diff = pA->curY - m_AIPlayer->curY;
	//Approach Player
	if(x_diff < -1)
		retVal = MOVE_LEFT;
	else if(x_diff > 1)
		retVal = MOVE_RIGHT;
	else if(y_diff < -1)
		retVal = MOVE_UP;
	else if(y_diff > 1)
		retVal = MOVE_DOWN;
	//Let's do this the hard way
	if((y_diff == -1 && x_diff == 0) || //Player is on the top
		(y_diff == 1 && x_diff == 0) || //Player is on the bottom
		(x_diff == -1 && y_diff == 0) || //Player is on the left
		(x_diff == 1 && y_diff == 0))
	{
		retVal = MOVE_ATTACK;
	}
	//This will haunt the player
	if(x_diff == 1 && y_diff == 1)
		retVal = rand()%2 == 0?MOVE_DOWN:MOVE_RIGHT;
	else if(x_diff == 1 && y_diff == -1)
		retVal = rand()%2 == 0?MOVE_UP:MOVE_RIGHT;
	else if(x_diff == -1 && y_diff == 1)
		retVal = rand()%2 == 0? MOVE_LEFT:MOVE_DOWN;
	else if(x_diff == -1 && y_diff == -1)
		retVal = rand()%2 == 0?MOVE_LEFT:MOVE_UP;
	if(lastMove == retVal)
		movetries++;
	else
	{
		lastMove = retVal;
		movetries = 0;
	}
	if(movetries >= 3)
	{
		int randMove = rand() %  MOVE_COUNT;
		while(randMove == lastMove || randMove == MOVE_ATTACK)
			randMove = rand() %  MOVE_COUNT;
		retVal = (EMOVE)randMove;
		movetries = 0;
	}
	if(x_diff == 0) //Above or Below
		m_AIPlayer->face = y_diff >= 0? FACE_DOWN:FACE_UP;
	if(y_diff == 0) //Beside
		m_AIPlayer->face = x_diff >= 0? FACE_RIGHT:FACE_LEFT;
	return retVal;
}